package com.smart.sso.server.controller.admin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Pagination;
import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.sso.server.controller.common.BaseController;
import com.smart.sso.server.log.SystemControllerLog;
import com.smart.sso.server.model.RegisterUser;
import com.smart.sso.server.model.User;
import com.smart.sso.server.service.RegisterUserService;
import com.smart.sso.server.service.UserService;
import com.smart.sso.server.util.OperationUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author YM
 */
@Api(tags = "用户审核")
@Controller
@RequestMapping("/admin/userAudit")
public class UserAuditController extends BaseController {
	
	@Resource
	private RegisterUserService registerUserService;
	
	@Resource
	private UserService userService;
	
	@ApiOperation("初始页")
	@RequestMapping(method = RequestMethod.GET)
	public String userAudit(Model model) {
		return "/admin/userAudit";
	}
	
	@ApiOperation("列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody Result list(
			@ApiParam(value = "登录名") String account,
			@ApiParam(value = "开始页码", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer pageNo,
			@ApiParam(value = "显示条数", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer pageSize) {
		if(null==pageNo)
		{
			pageNo=1;
		}
		if(null==pageSize)
		{
			pageSize=20;
		}
		return Result.createSuccessResult().setData(registerUserService.findPaginationByAccount(account, new Pagination<RegisterUser>(pageNo, pageSize)));
	}
	
	@ApiOperation("拒绝申请")
	@RequestMapping(value = "/refuse", method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_user_audit_refuse ,description = "拒绝用户注册申请")
	public @ResponseBody Result refuse(
			@ApiParam(value = "ids", required = true) @ValidateParam({ Validator.NOT_BLANK }) String ids) {
		registerUserService.deleteById(getAjaxIds(ids));
		return Result.createSuccessResult();
	}
	
	@ApiOperation("通过申请")
	@RequestMapping(value = "/accept", method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_user_audit_accept ,description = "通过用户注册申请")
	public @ResponseBody Result accept(
			@ApiParam(value = "ids", required = true) @ValidateParam({ Validator.NOT_BLANK }) String ids) {
		List<RegisterUser> registerUserList = registerUserService.findById(getAjaxIds(ids));
		for (RegisterUser registerUser : registerUserList)
		{
			User user = new User();
			user.setAccount(registerUser.getAccount());
			user.setPassword(registerUser.getPassword());
			user.setIsEnable(true);
			user.setCreateTime(new Date());
			userService.save(user, getAjaxIds(registerUser.getRoleIds()));
		}
		registerUserService.deleteById(getAjaxIds(ids));
		return Result.createSuccessResult();
	}

}
